﻿using System;
using System.Collections.Generic;
using System.Web;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.FileSystem
{
    public class FileSystemConfigControl : AbstractConfigControl
    {
        private FileSystemConfig _config;
        private TextBox rootDirTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                return new FileSystemConfig
                {
                    RootDir = rootDirTextBox.Text
                }
                .SerializeToJson();
            }
        }

        public FileSystemConfigControl(string config)
        {
            _config = config.DeserializeJsonTo<FileSystemConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                rootDirTextBox.Text = _config.RootDir;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            rootDirTextBox.ID = "rootDirTextBox";

            rootDirTextBox.CssClass = "guiInputText guiInputStandardSize";

            Controls.Add(rootDirTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("Root Directory:", "Please enter the relative path to the root directory", rootDirTextBox);
        }
    }
}